package com.hgd.security.evp.auth.authentication;

import com.alibaba.fastjson.JSONObject;
import com.hgd.security.evp.auth.entity.CustomUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义登录失败响应
 * @author By 尚
 * @date 2019/3/30 17:10
 */
@Component
@Slf4j
public class HgdAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {
        log.info("登录失败");
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());

        JSONObject result=new JSONObject();
        result.put("message", exception.getMessage());

        response.getWriter().write(result.toJSONString());
    }
}
